package it.polimi.reTravelDream.beans;



import java.util.ArrayList;
import java.util.List;

import it.polimi.reTravelDream.dto.FlightDTO;
import it.polimi.reTravelDream.eao.FlightEAOLocal;
import it.polimi.reTravelDream.eao.TravelPackageActionEAOLocal;

import javax.ejb.EJB;
import javax.enterprise.context.RequestScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

@ManagedBean(name="flight")
@SessionScoped

public class FlightBean {
	
	@EJB
	private FlightEAOLocal flightAction;
	
	
	private FlightDTO currentFlight;
	
	private String errorId;
	private String errorDepartureCity;
	private String erroreArrivalCity;
	private String errorDate;
	private String errorPrice;
	
	private String packageCreated;
	
	public FlightBean(){
		this.currentFlight=new FlightDTO();
	}
	
	public void addFlight(){
		if (!this.check()) {
			return;
		}
		try {
			this.currentFlight.setActive(true);
			this.flightAction.addFlight(this.currentFlight);
			this.currentFlight=new FlightDTO();
			this.packageCreated="Package Created!";
		} catch (Exception e) {
			System.out.println(e);
		}
	}
	
	private boolean check(){
		boolean value=true;
		this.packageCreated="";
		this.errorId="";
		this.errorDepartureCity="";
		this.erroreArrivalCity="";
		this.errorDate="";
		this.errorPrice="";
		
		System.out.println(this.currentFlight.getId());
		System.out.println(this.currentFlight.getPrice());
		
		if (!this.checkDepartureCity()) {
			this.errorDepartureCity="you must insert a city";
			value=false;
			System.out.println("departure city non valido");
		}
		if (!this.checkArrivalCity()) {
			this.erroreArrivalCity="you must insert a city";
			value=false;
			System.out.println("arrival city non valido");

		}
		if(!this.checkDate()){
			this.errorDate="you must insert a date";
			value=false;
			System.out.println("date non valido");
		}
		
		if(!this.checkPrice()){
			this.errorPrice="you must insert a valid price";
			System.out.println("price  non valido");
			value=false;
		}
		
		List<FlightDTO> flightDTOs=new ArrayList<FlightDTO>();
		flightDTOs=this.flightAction.getAllFlights();
		System.out.println(flightDTOs);
		
		for(FlightDTO fDto:flightDTOs){
			System.out.println("verificoId");
			if(this.currentFlight.getId()==fDto.getId()){
				this.errorId="id already used";
				value=false;
				System.out.println("id non valido");
			}
		}
		return value;
	}
	
	
	private boolean checkDepartureCity(){
		if(this.currentFlight.getDepartureCity().isEmpty()){
			return false;
		}
		return true;
	}
	
	private boolean checkArrivalCity(){
		if(this.currentFlight.getArrivalCity().isEmpty()){
			return false;
		}
		return true;
	}
	
	private boolean checkDate(){
		if(this.currentFlight.getData()==null){
			return false;
		}
		return true;
	}
	private boolean checkPrice(){
		if (this.currentFlight.getPrice()==0) {
			return false;
		}
		return true;
	}

	public FlightDTO getCurrentFlight() {
		return currentFlight;
	}

	public void setCurrentFlight(FlightDTO currentFlight) {
		this.currentFlight = currentFlight;
	}

	public String getErrorId() {
		return errorId;
	}

	public void setErrorId(String errorId) {
		this.errorId = errorId;
	}

	public String getErrorDepartureCity() {
		return errorDepartureCity;
	}

	public void setErrorDepartureCity(String errorDepartureCity) {
		this.errorDepartureCity = errorDepartureCity;
	}

	public String getErroreArrivalCity() {
		return erroreArrivalCity;
	}

	public void setErroreArrivalCity(String erroreArrivalCity) {
		this.erroreArrivalCity = erroreArrivalCity;
	}

	public String getErrorDate() {
		return errorDate;
	}

	public void setErrorDate(String errorDate) {
		this.errorDate = errorDate;
	}

	public String getErrorPrice() {
		return errorPrice;
	}

	public void setErrorPrice(String errorPrice) {
		this.errorPrice = errorPrice;
	}

	public String getPackageCreated() {
		return packageCreated;
	}

	public void setPackageCreated(String packageCreated) {
		this.packageCreated = packageCreated;
	}
	
}